define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product_apply/index' + location.search,
                    add_url: 'product_apply/add',
                    edit_url: 'product_apply/edit',
                    del_url: 'product_apply/del',
                    multi_url: 'product_apply/multi',
                    import_url: 'product_apply/import',
                    table: 'product_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'type_text', title: "取件方式"},
                        {field: 'address', title: '收货地址'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'sn',
                                title: "绑定",
                                text: __('绑定'),
                                icon: 'fa fa-chain',
                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                url: 'product_apply/sn',
                                extend: 'data-area=\'["100%", "100%"]\''
                            },{
                                name: 'confirm',
                                text: __('确认'),
                                title: __('确认'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa fa-check',
                                url: 'product_apply/confirm',
                                confirm: '是否确认?',
                                success: function (data, ret) {
                                    table.bootstrapTable('refresh', {});
                                }
                            },{
                                name: 'divide',
                                title: "分配",
                                text: __('分配'),
                                icon: 'fa fa-code-fork',
                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                url: 'product_apply/divide',
                                extend: 'data-area=\'["100%", "100%"]\''
                            }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sn: function () {
            Controller.api.bindevent();
        },
        divide: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
